<?php


class core_remote_getcontent {

	var $recv_headers='';
	var $send_headers = array();

	function setheaders($headers=array()) {
		$this->send_headers = $headers;
	}

	function getheaders() {
		return $this->recv_headers;
	}

	function send($url,$data=array()) {
		
		$headers = array();
		foreach($this->send_headers as $k=>$v){
			$headers[strtolower($k)]=$k.': '.$v;
		}
		
		if(empty($data)){
			$http=array('method'=>'GET','header'=>implode("\r\n",$headers)."\r\n");
		}else{
			if(!isset($headers['content-type'])){
				$headers['content-type'] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
			}

			$data_url = is_array($data) ? http_build_query($data) : $data;
			unset($data);
			$headers['content-length'] = 'Content-Length: '.strlen($data_url);
			$http=array('method'=>'POST','header'=>implode("\r\n",$headers)."\r\n",'content'=>$data_url);
			unset($data_url);
		}
		unset($headers);
		
		$content = file_get_contents($url,false,stream_context_create(array('http'=>$http)));
		unset($http);
		
		$this->recv_headers = $http_response_header;
		
		return $content;
	}
}